package com.chen.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.chen.vo.AddressVo;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AddressUtils {

    public static List<AddressVo> jsonToAddressList() {
        File outFile = new File("city.json");
        ClassPathResource resource = new ClassPathResource("static/city.json");
        if(outFile.exists()){
            outFile.delete();
        }
        List<AddressVo> addressVoList = new ArrayList<>();
        try {
            InputStream inputStream = resource.getInputStream();
            FileUtils.copyInputStreamToFile(inputStream,outFile);
            String s = FileUtils.readFileToString(outFile);
            JSONArray dataJson = new JSONArray(s);
            for (int i = 0; i < dataJson.size(); i++) {
                JSONObject o = dataJson.getJSONObject(i);
                JSONObject object = new JSONObject(o,"parent","treeID","name");
                AddressVo addressVo = object.toBean(AddressVo.class);
                addressVoList.add(addressVo);
            }
            return addressVoList;
        }catch (IOException e){
            e.printStackTrace();
        }
        return addressVoList;
    }
    
//    public List<AddressOptions> addressVoConvertToAddressOption(List<AddressVo> addressVoList){
//        AddressOptions options = new AddressOptions();
//        List<AddressOptions> optionsList = new ArrayList<>();
//        for (AddressVo addressVo : addressVoList) {
//            options.setValue(addressVo.getTreeID());
//            options.setLabel(addressVo.getName());
//            options.getChildren(addressVo.getChildren());
//        }
//    }

}
